package com.cssl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.pojo.*;
import com.cssl.pojo.Collections;
import com.cssl.service.*;
import com.cssl.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

//论坛
@RestController
public class ForumController {
     @Autowired
     private ForumService service;

     @Autowired
     private ReviewService service1;

     @Autowired
     private CollectionService service2;

     @Autowired
     private LikeService service3;

     @Autowired
     private UserService service4;

     @Autowired
     private RedisUtil redisUtil;

     @RequestMapping("/bbsAll")
     public IPage<Forum> bbsAll(@RequestParam Map<String,Object> map){
          int index = Integer.parseInt(map.get("index").toString());
          LambdaQueryWrapper<Forum> lqw  = Wrappers.lambdaQuery();
          lqw.like(Forum::getForumtitle,map.get("title").toString());
          lqw.eq(Forum::getFb_audit,1);
          lqw.orderByDesc(Forum::getFbdate);
          return service.page(new Page<>(index,12),lqw);
     }

     //添加帖子
     @RequestMapping("/addbbs")
     public Boolean addbbs(@RequestParam MultipartFile files, @RequestParam Map<String,Object> map) throws IOException {
          // 从请求上下文里获取Request对象
          ServletRequestAttributes requestAttributes = ServletRequestAttributes.class.cast(RequestContextHolder.getRequestAttributes());
          HttpServletRequest request = requestAttributes.getRequest();
          HttpSession session = request.getSession();
          User u =(User)redisUtil.get(session.getId());
          Forum fm = new Forum();
          fm.setForumtitle(map.get("title").toString());
          fm.setForumdetail(map.get("detail").toString());
          fm.setFb_uid(u.getUid());
          String path = "D:\\nginx\\nginx-1.25.0\\html\\pet\\images\\";
          List<String> types =new ArrayList<>();
          types.add("image/jpeg");
          types.add("image/gif");
          types.add("image/png");
          String type = files.getContentType();
          double size = files.getSize() * 1.0 / 1024 / 1024;
          String fileName = UUID.randomUUID().toString() + files.getOriginalFilename();
          System.out.println("filename:"+fileName);
          fm.setFb_file(fileName);
          if (types.contains(type) && size <= 3) {
               files.transferTo(new File(path + fileName));
               return service.save(fm);
          } else {
               System.out.println("文件类型或大小不允许");
          }
          return false;

     }


     //查看帖子增加浏览量
     @RequestMapping("/addreadnum")
     public Boolean addreadnum(@RequestParam Integer forumid){
          return service.addreadnum(forumid);
     }

     //浏览最多显示
     @RequestMapping("/readnumshow")
     public IPage<Forum> readnumshow(){
          LambdaQueryWrapper<Forum> lqw  = Wrappers.lambdaQuery();
          lqw.orderByDesc(Forum::getReadnum);
          return service.page(new Page<>(0,10),lqw);
     }

     //喜欢最多显示
     @RequestMapping("/likenumshow")
     public IPage<Like> likenumshow(){
          LambdaQueryWrapper<Like> lqw  = Wrappers.lambdaQuery();
          lqw.orderByDesc(Like::getLikedate);
          return service3.page(new Page<>(0,10),lqw);
     }
     //收藏最多显示
     @RequestMapping("/collnumshow")
     public IPage<Collections> collnumshow(){
          LambdaQueryWrapper<Collections> lqw  = Wrappers.lambdaQuery();
          lqw.orderByDesc(Collections::getCodate);
          return service2.page(new Page<>(0,10),lqw);
     }
     //评论最多显示
     @RequestMapping("/reviewnumshow")
     public IPage<Review> reviewnumshow(){
          LambdaQueryWrapper<Review> lqw  = Wrappers.lambdaQuery();
          lqw.orderByDesc(Review::getRedate);
          return service1.page(new Page<>(0,10),lqw);
     }


     //查看贴子详情
     @RequestMapping("/bbsdetail")
     public Forum readnumshow(@RequestParam Integer forumid){
          return service.getById(forumid);
     }

     //显示贴子的评论
     @RequestMapping("/reviewshow")
     public IPage<Review> reviewshow(@RequestParam Map<String,Object> map){
          LambdaQueryWrapper<Review> lqw  = Wrappers.lambdaQuery();
          lqw.eq(Review::getR_forumid,Integer.parseInt(map.get("forumid").toString()));
          return service1.page(new Page<>(Integer.parseInt(map.get("index").toString()),5),lqw);
     }

     @RequestMapping("/addreview1")
     public Boolean addreview1(@RequestParam Map<String,Object> map,HttpServletRequest request) throws IOException {
          HttpSession session = request.getSession();
          User u =(User)redisUtil.get(session.getId());
          Review rw = new Review();
          rw.setR_forumid(Integer.parseInt(map.get("forumid").toString()));
          rw.setReviewdetail(map.get("detail").toString());
          rw.setRe_uid(u.getUid());//评论帖子的用户
          rw.setRef_uid(Integer.parseInt(map.get("uid").toString()));//发帖用户
          return service1.save(rw);
     }


     //添加评论(加图片)
     @RequestMapping("/addreview2")
     public Boolean addreview2(@RequestParam MultipartFile files, @RequestParam Map<String,Object> map) throws IOException {
          // 从请求上下文里获取Request对象
          ServletRequestAttributes requestAttributes = ServletRequestAttributes.class.cast(RequestContextHolder.getRequestAttributes());
          HttpServletRequest request = requestAttributes.getRequest();
          HttpSession session = request.getSession();
          User u =(User)redisUtil.get(session.getId());

          Review rw = new Review();
          rw.setR_forumid(Integer.parseInt(map.get("forumid").toString()));
          rw.setReviewdetail(map.get("detail").toString());
          rw.setRe_uid(u.getUid());//评论
          rw.setRef_uid(Integer.parseInt(map.get("uid").toString()));//发帖
          String path = "D:\\nginx\\nginx-1.25.0\\html\\pet\\images\\";
          List<String> types =new ArrayList<>();
          types.add("image/jpeg");
          types.add("image/gif");
          types.add("image/png");
          String type = files.getContentType();
          double size = files.getSize() * 1.0 / 1024 / 1024;
          String fileName = UUID.randomUUID().toString() + files.getOriginalFilename();
          System.out.println("filename:"+fileName);
          rw.setRe_file(fileName);
          if (types.contains(type) && size <= 3) {
               files.transferTo(new File(path + fileName));
               return service1.save(rw);
          } else {
               System.out.println("文件类型或大小不允许");
          }
          return false;

     }

     //收藏帖子
     @RequestMapping("/addcoll")
     public Boolean collection(@RequestParam Map<String,Object> map, HttpServletRequest request){
          HttpSession session = request.getSession();
          User u = (User)redisUtil.get(session.getId());
          Collections cs = new Collections();
          cs.setCo_forumid(Integer.parseInt(map.get("forumid").toString()));
          cs.setCo_uid(u.getUid());
          cs.setCo_fuid(Integer.parseInt(map.get("uid").toString()));

          User u1 = service4.getById(Integer.parseInt(map.get("uid").toString()));
          cs.setFabuuser(u1.getUsername());//发帖用户
          User u2 = service4.getById(u.getUid());
          cs.setColluser(u2.getUsername());//收藏用户
          Forum fm =service.getById(Integer.parseInt(map.get("forumid").toString()));
          cs.setFabutitle(fm.getForumtitle());//标题

          return service2.save(cs);
     }
     //验证是否已添加收藏
     @RequestMapping("/yzcoll")
     public Boolean yzcoll(@RequestParam Map<String,Object> map,HttpServletRequest request) throws IOException {
          HttpSession session =request.getSession();
          User u = (User)redisUtil.get(session.getId());
          LambdaQueryWrapper<Collections> lqw = Wrappers.lambdaQuery();
          lqw.eq(Collections::getCo_uid,u.getUid());
          lqw.eq(Collections::getCo_forumid,Integer.parseInt(map.get("forumid").toString()));
          Collections lk = service2.getOne(lqw);
          if(lk != null){
               return true;
          }
          return false;
     }

     //删除喜欢帖子
     @RequestMapping("/delcoll")
     public Boolean delcoll(@RequestParam Map<String,Object> map,HttpServletRequest request) throws IOException {
          HttpSession session =request.getSession();
          User u = (User)redisUtil.get(session.getId());
          LambdaQueryWrapper<Collections> lqw = Wrappers.lambdaQuery();
          lqw.eq(Collections::getCo_uid,u.getUid());
          lqw.eq(Collections::getCo_forumid,Integer.parseInt(map.get("forumid").toString()));
          return service2.remove(lqw);
     }
     //添加我喜欢
     @RequestMapping("/addlike")
     public Boolean addlike(@RequestParam Map<String,Object> map,HttpServletRequest request) throws IOException {
          HttpSession session =request.getSession();
          User u = (User)redisUtil.get(session.getId());
          Like lk = new Like();
          lk.setLike_fuid(Integer.parseInt(map.get("uid").toString()));
          lk.setLike_uid(u.getUid());
          lk.setLike_forumid(Integer.parseInt(map.get("forumid").toString()));

          User u1 = service4.getById(Integer.parseInt(map.get("uid").toString()));
          lk.setFtuser(u1.getUsername());//发帖用户
          User u2 = service4.getById(u.getUid());
          lk.setLikeuser(u2.getUsername());//点赞用户
          Forum fm =service.getById(Integer.parseInt(map.get("forumid").toString()));
          lk.setFtitle(fm.getForumtitle());//标题
          return service3.save(lk);
     }

     //验证是否已添加喜欢
     @RequestMapping("/yzlike")
     public Boolean yzlike(@RequestParam Map<String,Object> map,HttpServletRequest request) throws IOException {
          HttpSession session =request.getSession();
          User u = (User)redisUtil.get(session.getId());
          LambdaQueryWrapper<Like> lqw = Wrappers.lambdaQuery();
          lqw.eq(Like::getLike_uid,u.getUid());
          lqw.eq(Like::getLike_forumid,Integer.parseInt(map.get("forumid").toString()));
          Like lk = service3.getOne(lqw);
          if(lk != null){
               return true;
          }
          return false;
     }
     //删除喜欢帖子
     @RequestMapping("/dellike")
     public Boolean dellike(@RequestParam Map<String,Object> map,HttpServletRequest request) throws IOException {
          HttpSession session =request.getSession();
          User u = (User)redisUtil.get(session.getId());
          LambdaQueryWrapper<Like> lqw = Wrappers.lambdaQuery();
          lqw.eq(Like::getLike_uid,u.getUid());
          lqw.eq(Like::getLike_forumid,Integer.parseInt(map.get("forumid").toString()));
          return service3.remove(lqw);
     }
    //帖子点赞量
    @RequestMapping("/likenum")
    public Long likenum(@RequestParam Integer forumid){
          LambdaQueryWrapper<Like> lqw = Wrappers.lambdaQuery();
          lqw.eq(Like::getLike_forumid,forumid);
         return service3.count(lqw);
    }
     //帖子评论量
     @RequestMapping("/reviewnum")
     public Long reviewnum(@RequestParam Integer forumid){
          LambdaQueryWrapper<Review> lqw = Wrappers.lambdaQuery();
          lqw.eq(Review::getR_forumid,forumid);
          return service1.count(lqw);
     }
     //帖子收藏量
     @RequestMapping("/collnum")
     public Long collnum(@RequestParam Integer forumid){
          LambdaQueryWrapper<Collections> lqw = Wrappers.lambdaQuery();
          lqw.eq(Collections::getCo_forumid,forumid);
          return service2.count(lqw);
     }

     //添加帖子（不加图片）
     @RequestMapping("/addforum1")
     public Boolean addforum1(@RequestParam Map<String,Object> map,HttpServletRequest request){
         HttpSession session = request.getSession();
         User u =(User)redisUtil.get(session.getId());
          Forum fm = new Forum();
          fm.setFb_uid(u.getUid());
          fm.setForumtitle(map.get("title").toString());
          fm.setForumdetail(map.get("detail").toString());
          return service.save(fm);
     }
     //添加帖子
     @RequestMapping("/addforum2")
     public Boolean addforum2(@RequestParam MultipartFile files, @RequestParam Map<String,Object> map) throws IOException {
          // 从请求上下文里获取Request对象
          ServletRequestAttributes requestAttributes = ServletRequestAttributes.class.cast(RequestContextHolder.getRequestAttributes());
          HttpServletRequest request = requestAttributes.getRequest();
          HttpSession session = request.getSession();
          User u =(User)redisUtil.get(session.getId());

          Forum fm = new Forum();
          fm.setFb_uid(u.getUid());
          fm.setForumtitle(map.get("title").toString());
          fm.setForumdetail(map.get("detail").toString());

          String path = "D:\\nginx\\nginx-1.25.0\\html\\pet\\images\\";
          List<String> types =new ArrayList<>();
          types.add("image/jpeg");
          types.add("image/gif");
          types.add("image/png");
          String type = files.getContentType();
          double size = files.getSize() * 1.0 / 1024 / 1024;
          String fileName = UUID.randomUUID().toString() + files.getOriginalFilename();
          System.out.println("filename:"+fileName);
          fm.setFb_file(fileName);
          if (types.contains(type) && size <= 3) {
               files.transferTo(new File(path + fileName));
               return service.save(fm);
          } else {
               System.out.println("文件类型或大小不允许");
          }
          return false;

     }
}
